#!/bin/sh

export 'PS4=[`date +"%Y-%m-%d %H:%M:%S"`]`basename $0`.$LINENO+ '


if [ ! -d $1 ]; then
    mkdir -p $1
fi

cd $1

patch_list=`git diff origin/master --name-status HEAD~1 HEAD~0 | grep -E "*\.patch$" | grep -E "^A|^R" |  awk '{print $2}'`
patch_del_list=`git diff origin/master --name-status HEAD~1 HEAD~0 | grep -E "*\.patch$" | grep -E "^D" |  awk '{print $2}'`
ret=0
error_file="${WORKSPACE}/error_file"
add_patch_list="${WORKSPACE}/add_patch_list"
echo "Error list:" > $error_file
touch ${WORKSPACE}/add_patch_list

# check add patch
function spec_add_patch()
{
    patch=$1
    specfile=$2
    #check patch name
    if echo "$patch" | grep -E "^huawei-|^backport-" &> /dev/null; then
        echo "The name of the $patch  is OK"
    else
        commit_num=`git log --pretty=oneline | wc -l`
        if [ $commit_num -gt 2 ];then
            echo "[Error]: Usually, the patchs in the commit should be named biginning with 'huawei-' or 'backport-';if you want to continue still,the patch [$patch] should be reviewed by PM/SE,then contack CI" >> $error_file
            ret=1
        else
            echo "This commit is to init, so OK"
        fi
    fi
    #check the sequence that the patch been applied in specfiles
    sequence=$(cat $specfile | grep "$patch" | awk -F ':' '{print $1}' | awk -F 'Patch' '{print $2}')
    if [ "x$sequence" != "x"  ]; then
	    if echo $patch | grep "^huawei-" &> /dev/null; then
            if [ $sequence -lt 9000 ];then
                echo "[Error]: Patch $patch shoulb be applied between Patch9000 and Patch100000 in specfile" >> $error_file
                ret=1
            fi
	    else
            if [ $sequence -lt 6000 -o $sequence -gt 8999 ];then
                echo "[Error]: Patch $patch shoulb be applied between Patch6000 and Patch8999 in specfile" >> $error_file
                ret=1
            fi
	    fi
    else
        echo "[Error]: Patch $patch shoulb be applied Patch + number  in specfile. eg: Patch9000 " >> $error_file
        ret=1
    fi
}

#对于新增加的patch，首先判断在spec和series.conf文件，是否新增。
#如果都没有增加，或都增加，或增加多次，都会导致失败
function add_patch()
{
    path_patch=$1
    patch="${path_patch##*/}"
    series_add="false"
    spec_add="true"
    series_exsit="true"
    spec_exsit="true"
    series_list=$(find . -name "series*.conf")
    if [ "x$series_list" != "x" ];then
        for series in $(echo "$series_list")
        do
            series_add="true"
            if [ $(cat $series | grep $patch |wc -l) -ne 1 ];then
                series_add="false"
            fi
        done
    else
        series_exsit="false"
    fi

    spec_list=$(find . -name  "*.spec")
    if [ "x$spec_list" != "x" ];then
        for spec in $(echo $spec_list)
        do
            #如果patch在spec没有增加或增加多次，就会失败。
            num=$(cat $spec |grep -iE  "Patch[0-9]*:[[:space:]]*$patch" |wc -l)
            if [ $num -eq 0 ];then
                spec_add="false"
            elif [ $num -eq 1 ];then
                if [ "x$GERRIT_BRANCH" != "xnext_openeuler" ];then
                   spec_add_patch $patch $spec
                fi
            fi
        done
    else
        spec_exsit="false"
    fi

    if [ "$series_add" = "false" -a "$spec_add" = "false" ];then
        echo "[Error]: You must add $patch in spec file or series.conf file" >> $error_file
        ret=1
    elif [ "$series_add" = "true" -a "$spec_add" = "true" -a "$series_exsit" = "true" -a "$spec_exsit" = "true" ];then
        echo "[Error]: You can not add $patch in spec file and series.conf file at the same time " >> $error_file
        ret=1
    fi

}

#对于删除的patch，首先判断在spec和series.conf文件，是否删除。
#如果都没有删除，或只有一个删除，都会导致失败
function del_patch()
{
    path_patch=$1
    patch="${path_patch##*/}"
    if [ "x$GERRIT_BRANCH" != "xnext_openeuler" ];then
	    if echo "$patch" | grep -E "^huawei-" &> /dev/null; then
		echo "[Warning]: You are going to delete some huawei-patchs ,please confirm it manually." >> $error_file
		ret=1
            fi   
    fi
    series_list=$(find . -name "series*.conf")
    if [ "x$series_list" != "x" ];then
        for series in $(echo "$series_list")
        do
            if [ "x$(cat $series | grep $patch)" != "x"  ];then
                 series_patch=$(cat $series | grep $patch)
            else
                 continue
            fi
            path="${series_patch%/*}"
            if [ "$path" == "$patch" ];then
                if [ $(cat $series | grep $path |wc -l) -ne 0 ];then
                    echo "[Error]:You must delete $path in $series file" >> $error_file
                    ret=1
                fi
            else 
                if [ $(cat $series | grep $path_patch |wc -l) -ne 0 ];then
                     echo "[Error]:You must delete $path in $series file" >> $error_file
                    ret=1
                fi

            fi 
        done
    fi

    spec_list=$(find . -name "*.spec")
    if [ "x$spec_list" != "x" ];then
        for spec in $(echo "$spec_list")
        do
            num=$(cat $spec |grep -iE  "Patch[0-9]*:[[:space:]]*$patch" |wc -l)
            if [ $num -ne 0 ];then
                echo "[Error]: You must delete $patch in $spec file" >> $error_file
                ret=1
            fi
        done
    fi

}

if [ "x$patch_list" != "x"  ];then
    for  patch in $(echo $patch_list)
    do
        if echo "$patch" | grep "lvs/" &> /dev/null;then
            echo "It's lvs,skip"
            break
        fi
        del_patch $patch
        echo "$patch" >>$add_patch_list
    done
fi


if [ "x$patch_del_list" != "x" ];then
    for patch in $(echo $patch_del_list)
    do
        if [ $(cat $add_patch_list |grep ${patch##*/} |wc -l ) -eq 0 ];then
            add_patch  $patch
        else
            continue
        fi
    done
fi
[ -n "${add_patch_list}" ] &&  rm -rf $add_patch_list
if [ -f $error_file ];then
    cat $error_file
fi
exit $ret
